﻿Public Class WfMinhasOS
    Inherits System.Web.UI.Page
    Dim despLogado As New Despachante
    Dim cod_os As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        despLogado = Session("Desplogado")
        AlterarOs.Visible = False
        If IsPostBack = False Then

        End If

    End Sub

    Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        If Session("DespLogado") Is Nothing Then
            Response.Redirect("/Screens/WF1.aspx")
        End If
    End Sub

    Public Sub PopulaOsTodas()
        Dim objOsDao As New OsDAO
        gvOs.AutoGenerateColumns = False
        GvOs.DataSource = objOsDao.OsJoinTodas(despLogado)
        GvOs.DataBind()
    End Sub


    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim objOsDao As New OsDAO
        Dim dados As New DataTable
        lbmsg.Text = ""
        gvOs.DataBind()

        If DpBusca.SelectedItem.Text = "Data" Then
            Try

                gvOs.AutoGenerateColumns = False
                dados = objOsDao.OsJoinData(despLogado, Calendario.SelectedDate.Date)
                If dados.Rows.Count <> 0 Then
                    Calendario.Visible = False
                    gvOs.DataSource = dados
                    gvOs.DataBind()
                Else
                    lbmsg.ForeColor = Drawing.Color.Red
                    lbmsg.Text = "Nenhum resultado encontrado !"
                End If

            Catch ex As Exception
            End Try
        End If

        If DpBusca.SelectedItem.Text = "Nome Cliente" Then
            Try
                If tbBusca.Text <> "" Then
                    gvOs.AutoGenerateColumns = False
                    dados = objOsDao.OsJoinNome(despLogado, tbBusca.Text)
                    If dados.Rows.Count <> 0 Then
                        gvOs.DataSource = dados
                        gvOs.DataBind()
                    Else
                        lbmsg.ForeColor = Drawing.Color.Red
                        lbmsg.Text = "Nenhum resultado encontrado !"
                    End If
                Else
                    lbmsg.ForeColor = Drawing.Color.Red
                    lbmsg.Text = "Digite o Nome !"
                End If
            Catch ex As Exception
                lbmsg.ForeColor = Drawing.Color.Red
                lbmsg.Text = "Digite apenas Letras !"
            End Try

        End If

        If DpBusca.SelectedItem.Text = "Código" Then
            Try
                If tbBusca.Text <> "" Then
                    gvOs.AutoGenerateColumns = False
                    dados = objOsDao.OsJoinCodigo(despLogado, tbBusca.Text)
                    If dados.Rows.Count <> 0 Then
                        gvOs.DataSource = dados
                        gvOs.DataBind()
                    Else
                        lbmsg.ForeColor = Drawing.Color.Red
                        lbmsg.Text = "Nenhum resultado encontrado !"
                    End If
                Else
                    lbmsg.ForeColor = Drawing.Color.Red
                    lbmsg.Text = "Digite o Código !"
                End If
            Catch ex As Exception
                lbmsg.ForeColor = Drawing.Color.Red
                lbmsg.Text = "Digite apenas Números !"
            End Try

        End If

        If DpBusca.SelectedItem.Text = "Todas" Then
            PopulaOsTodas()
        End If

        If DpBusca.SelectedItem.Text = "Status" Then
            Try
                If tbBusca.Text <> "" Then
                    gvOs.AutoGenerateColumns = False
                    dados = objOsDao.OsJoinStatus(despLogado, tbBusca.Text)
                    If dados.Rows.Count <> 0 Then
                        gvOs.DataSource = dados
                        gvOs.DataBind()
                    Else
                        lbmsg.ForeColor = Drawing.Color.Red
                        lbmsg.Text = "Nenhum resultado encontrado !"
                    End If
                Else
                    lbmsg.ForeColor = Drawing.Color.Red
                    lbmsg.Text = "Digite o Status !"
                End If
            Catch ex As Exception
                lbmsg.ForeColor = Drawing.Color.Red
                lbmsg.Text = "Digite apenas Letras !"
            End Try

        End If
    End Sub

    
    Protected Sub gvOs_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvOs.PageIndexChanging
        gvOs.PageIndex = e.NewPageIndex
        PopulaOsTodas()
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DpBusca.SelectedIndexChanged
        If DpBusca.SelectedItem.Text = "Data" Then
            Calendario.Visible = True
            tbBusca.Enabled = False
        End If
        If DpBusca.SelectedItem.Text = "Código" Then
            Calendario.Visible = False
            tbBusca.Enabled = True

        End If
        If DpBusca.SelectedItem.Text = "Todas" Then
            Calendario.Visible = False
            tbBusca.Enabled = True

        End If
        If DpBusca.SelectedItem.Text = "Status" Then
            Calendario.Visible = False
            tbBusca.Enabled = True
        End If
        If DpBusca.SelectedItem.Text = "Nome Cliente" Then
            Calendario.Visible = False
            tbBusca.Enabled = True
        End If
    End Sub

    Protected Sub gvOs_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gvOs.SelectedIndexChanged

        AlterarOs.Visible = True
    End Sub

    Protected Sub AlterarOs_Click(sender As Object, e As EventArgs) Handles AlterarOs.Click
        cod_os = gvOs.SelectedRow.Cells(2).Text
        Response.Redirect("WfOS.aspx?CodOs=" & cod_os)
    End Sub
End Class